{% from "types/macros.rs.jinja" import field_name -%}
{% if type.name is endingwith "Patch" -%}
    use js_option::JsOption;
{% endif -%}
use serde::{Deserialize, Serialize};

use super::{
    {% for c in referenced_components -%}
        {{ c | to_snake_case }}::{{ c | to_upper_camel_case }},
    {% endfor -%}
};

{{ doc_comment }}
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct {{ type.name | to_upper_camel_case }} {
    {% include "types/struct_fields.rs.jinja" -%}
}

impl {{ type.name | to_upper_camel_case }} {
    pub fn new(
        {% for field in type.fields -%}
            {% if field.type.is_datetime() -%}
                {% set field_ty = "String" -%}
            {% else -%}
                {% set field_ty = field.type.to_rust() -%}
            {% endif -%}
            {% if field.required and not field.nullable -%}
                {{ field_name(field.name | to_snake_case) }}: {{ field_ty }},
            {% endif -%}
        {% endfor -%}
    ) -> Self {
        {% if type.fields | selectattr("deprecated") | length > 0 -%}
            #[allow(deprecated)]
        {% endif -%}
        Self {
            {% for field in type.fields -%}
                {% if field.required and not field.nullable -%}
                    {{ field_name(field.name | to_snake_case) }},
                {% elif type.name is endingwith "Patch" and field.nullable -%}
                    {{ field_name(field.name | to_snake_case) }}: JsOption::Undefined,
                {% else -%}
                    {{ field_name(field.name | to_snake_case) }}: None,
                {% endif -%}
            {% endfor -%}
        }
    }
}
